package com.back_end.Array.Print;

import java.util.ArrayList;
import java.util.Iterator;

public class ArraylistPrintTest {

    public static void main(String[] args) {
        ArrayList<String> stringArrayList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            stringArrayList.add(i+" ");
        }


        // 迭代器打印，1.先获取迭代器对象，2.调用迭代器对象的方法
        Iterator<String> iterator = stringArrayList.iterator();
        System.out.println("迭代器打印");
        while (iterator.hasNext()){
            System.out.print(iterator.next());
        }

        System.out.println();
        // 正常for打印
        System.out.println("正常for打印");
        for (int i = 0; i < stringArrayList.size(); i++) {
            System.out.print(stringArrayList.get(i));
        }
        System.out.println();

        // for each 打印
        System.out.println("for each 打印");
        for (String s : stringArrayList) {
            System.out.print(s);
        }


        System.out.println();
        // toArray用法
        // 第一种方式(最常用)
        String[] strings = stringArrayList.toArray(new String[0]);
        System.out.println("toArray"+strings[0]);

        // 第二种方式(容易理解)
        String[] strings1 = new String[stringArrayList.size()];
        stringArrayList.toArray(strings1);
        System.out.println("toArray1"+strings[0]);

        // 第三种方式
        Object[] o = stringArrayList.toArray();
        System.out.println("o" + o[1]);


        // 在指定位置添加元素
         stringArrayList.add(2,"8b8b8b8b8");

        System.out.println(stringArrayList.toString());

         // 删除指定位置上的元素
        stringArrayList.remove(2);
        System.out.println(stringArrayList.toString());

        // 删除指定元素
        stringArrayList.remove(3);
        System.out.println(stringArrayList.toString());


        // 判断arrayList是否包含5
        String ss = stringArrayList.get(5);
        System.out.println("ss"+ss);
        System.out.println(ss.equals("6"));
        System.out.println("stringArrayList contains ss：" + ss + "is: " + stringArrayList.contains(ss));


         // 清空ArrayList
        stringArrayList.clear();
         // 判断ArrayList是否为空
        System.out.println("stringArrayList is empty: " + stringArrayList.isEmpty());
    }

}
